// pages/cart/cart.js
import { Cart } from 'cart-model.js';

var cart = new Cart(); //实例化 购物车
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectedCounts: 0, //总的商品数
    selectedTypeCounts: 0, //总的商品类型数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var cartData = cart.getCartDataFromLocal(),
      countsInfo = cart.getCartTotalCounts(true);
    this.setData({
      selectedCounts: countsInfo.counts1,
      selectedTypeCounts: countsInfo.counts2,
      account: this._calcTotalAccountAndCounts(cartData).account,
      cartData: cartData
    });
  },

  /*更新购物车商品数据*/
  _resetCartData: function() {
    var newData = this._calcTotalAccountAndCounts(this.data.cartData); /*重新计算总金额和商品总数*/
    this.setData({
      account: newData.account,
      selectedCounts: newData.selectedCounts,
      selectedTypeCounts: newData.selectedTypeCounts,
      cartData: this.data.cartData
    });
  },

  /*
   * 计算总金额和选择的商品总数
   * */
  _calcTotalAccountAndCounts: function(data) {
    var len = data.length,
      account = 0,
      selectedCounts = 0,
      selectedTypeCounts = 0;
    let multiple = 100;
    for (let i = 0; i < len; i++) {
      //避免 0.05 + 0.01 = 0.060 000 000 000 000 005 的问题，乘以 100 *100
      if (data[i].selectStatus) {
        account += data[i].counts * multiple * Number(data[i].price) * multiple;
        selectedCounts += data[i].counts;
        selectedTypeCounts++;
      }
    }
    return {
      selectedCounts: selectedCounts,
      selectedTypeCounts: selectedTypeCounts,
      account: account / (multiple * multiple)
    }
  },


  /*调整商品数目*/
  changeCounts: function(event) {
    // var id = cart.getDataSet(event, 'id'),
    var id  = event.currentTarget.dataset.id
    var type = event.currentTarget.dataset.type
    // var type = cart.getDataSet(event, 'type'),
    var index = this._getProductIndexById(id),
      counts = 1;
    if (type == 'add') {
      cart.addCounts(id);
    } else {
      counts = -1;
      cart.cutCounts(id);
    }
    //更新商品页面
    this.data.cartData[index].counts += counts;
    this._resetCartData();
  },

  /*根据商品id得到 商品所在下标*/
  _getProductIndexById: function(id) {
    var data = this.data.cartData,
      len = data.length;
    for (let i = 0; i < len; i++) {
      if (data[i].id == id) {
        return i;
      }
    }
  },

  /*删除商品*/
  delete: function(event) {
    // var id = cart.getDataSet(event, 'id')
    var id =event.currentTarget.dataset.id
    var  index = this._getProductIndexById(id);
    this.data.cartData.splice(index, 1); //删除某一项商品

    this._resetCartData();
    //this.toggleSelectAll();

    cart.delete(id); //内存中删除该商品
  },

  /*选择商品*/
  toggleSelect: function(event) {
    var id = event.currentTarget.dataset.id
    var status = event.currentTarget.dataset.status
    var index = this._getProductIndexById(id);
    this.data.cartData[index].selectStatus = !status;
    this._resetCartData();
  },

  /*全选*/
  toggleSelectAll: function(event) {
    // var status = cart.getDataSet(event, 'status') == 'true';
    var status = event.currentTarget.dataset.status=='true'
    var data = this.data.cartData,
      len = data.length;
    for (let i = 0; i < len; i++) {
      data[i].selectStatus = !status;
    }
    this._resetCartData();
  },

  /*提交订单*/
  submitOrder: function() {
    var p  = this.data.cartData
    p = JSON.stringify(p)
    wx.navigateTo({
      url: '../order/order?product=' + p
    });
  },

  /*查看商品详情*/
  onProductsItemTap: function(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../home/detail/detail?id=' + id
    })
  },





  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    cart.execSetStorageSync(this.data.cartData);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})